﻿
//***********************************************************************
// * Module:   MsjHzHkzmbggzHandler
// * 作者:  XWG
// * Modified: 2013-05-29
// * Purpose: Definition of the Class MsjHzHkzmbggzHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using XWG.JWBST.JWBSTBLL;
using System.Collections;
using JWBSTAPP.CommonClass;
using XWG.Framework;

namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// MsjHzHkzmbggzHandler 的摘要说明
    /// </summary>
    public class MsjHzHkzmbggzHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {
      
        #region 添加记录
      /// <summary>
      /// 添加记录
      /// </summary>
      protected void AddRecord()
        {
             HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {           
            MsjHzHkzmbggz mMsjHzHkzmbggz = new MsjHzHkzmbggz();
            mMsjHzHkzmbggz.Sqid = Guid.NewGuid().ToString(); //主键
                 mMsjHzHkzmbggz.Sqrxm = context.Request.Form["texSqrxm"]; //申请人姓名
                 mMsjHzHkzmbggz.Sqrxb = context.Request.Form["selSqrxb"]; //申请人性别(字典)
                 mMsjHzHkzmbggz.Sqrcsrq = context.Request.Form["texSqrcsrq"]; //申请人出生日期
                 mMsjHzHkzmbggz.Sqrmz = context.Request.Form["texSqrmz"]; //申请人民族(字典)
                 mMsjHzHkzmbggz.Whcd = context.Request.Form["texWhcd"]; //文化程度(字典)
                 mMsjHzHkzmbggz.Sqrsfzh = context.Request.Form["texSqrsfzh"]; //申请人身份证号
                 mMsjHzHkzmbggz.Gzdw = context.Request.Form["texGzdw"]; //工作单位
                 mMsjHzHkzmbggz.Lxdh = context.Request.Form["texLxdh"]; //联系电话
                 mMsjHzHkzmbggz.Hkszd = context.Request.Form["texHkszd"]; //户口所在地
                 mMsjHzHkzmbggz.Ydjxm = context.Request.Form["texYdjxm"]; //原登记项目
                 mMsjHzHkzmbggz.Yqbggzhxm = context.Request.Form["texYqbggzhxm"]; //要求变更更正后项目
                 mMsjHzHkzmbggz.Sqbggzly = context.Request.Form["texSqbggzly"]; //申请变更更正理由
                 mMsjHzHkzmbggz.Sljg = context.Request.Form["texSljg"]; //受理机关(字典)
                 mMsjHzHkzmbggz.Cjsj = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); //创建时间(2013-05-28 09:13:01)
                 mMsjHzHkzmbggz.Cjr = Convert.ToString(context.Session["UserID"]);  //创建人(人员ID)
                 mMsjHzHkzmbggz.Enabled = Enabled.TRUE; //是否有效（1有效，0无效）
                 mMsjHzHkzmbggz.Movesign = "0"; //是否传输（1已，0未）
            
            mResultModel = MsjHzHkzmbggzBLL.Insert(mMsjHzHkzmbggz,context.Session["UserID"], true); //新建操作
            context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty);
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion
        
        #region 删除记录
      /// <summary>
      /// 删除记录
      /// </summary>
       protected void DeleteRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            string paramKey = context.Request.Params["id"];
            try
            {
              if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return ;
                }
                
                MsjHzHkzmbggz mMsjHzHkzmbggz = new MsjHzHkzmbggz(); 
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mResultModel = MsjHzHkzmbggzBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel));  //序列化后返回
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }            
        } 
        #endregion
     
        #region 修改记录
      /// <summary>
      /// 修改记录
      /// </summary>
      protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
           MsjHzHkzmbggz mMsjHzHkzmbggz = new MsjHzHkzmbggz();

            string paramKey = context.Request.Params["id"];
            try
            {
                
                if(string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return ;
                }
                   mMsjHzHkzmbggz = MsjHzHkzmbggzBLL.Load(paramKey); //根据键值获取实体信息
                    if (mMsjHzHkzmbggz != null)
                    {
                 mMsjHzHkzmbggz.Sqrxm = context.Request.Form["texSqrxm"]; //申请人姓名
                 mMsjHzHkzmbggz.Sqrxb = context.Request.Form["selSqrxb"]; //申请人性别(字典)
                 mMsjHzHkzmbggz.Sqrcsrq = context.Request.Form["texSqrcsrq"]; //申请人出生日期
                 mMsjHzHkzmbggz.Sqrmz = context.Request.Form["texSqrmz"]; //申请人民族(字典)
                 mMsjHzHkzmbggz.Whcd = context.Request.Form["texWhcd"]; //文化程度(字典)
                 mMsjHzHkzmbggz.Sqrsfzh = context.Request.Form["texSqrsfzh"]; //申请人身份证号
                 mMsjHzHkzmbggz.Gzdw = context.Request.Form["texGzdw"]; //工作单位
                 mMsjHzHkzmbggz.Lxdh = context.Request.Form["texLxdh"]; //联系电话
                 mMsjHzHkzmbggz.Hkszd = context.Request.Form["texHkszd"]; //户口所在地
                 mMsjHzHkzmbggz.Ydjxm = context.Request.Form["texYdjxm"]; //原登记项目
                 mMsjHzHkzmbggz.Yqbggzhxm = context.Request.Form["texYqbggzhxm"]; //要求变更更正后项目
                 mMsjHzHkzmbggz.Sqbggzly = context.Request.Form["texSqbggzly"]; //申请变更更正理由
                 mMsjHzHkzmbggz.Sljg = context.Request.Form["texSljg"]; //受理机关(字典)
                 //mMsjHzHkzmbggz.Cjsj = context.Request.Form["texCjsj"]; //创建时间(2013-05-28 09:13:01)
                 //mMsjHzHkzmbggz.Cjr = context.Request.Form["texCjr"]; //创建人(人员ID)
                 //mMsjHzHkzmbggz.Cjrdw = context.Request.Form["texCjrdw"]; //创建单位(单位ID)
                 mMsjHzHkzmbggz.Xgr = Convert.ToString(context.Session["UserID"]); //修改人(人员ID)
                 //mMsjHzHkzmbggz.Xgrdw = context.Request.Form["texXgrdw"]; //修改人单位(单位ID)
                 mMsjHzHkzmbggz.Xgsj = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");  //修改时间(2013-05-28 09:13:01)
                 mMsjHzHkzmbggz.Enabled = context.Request.Form["selEnabled"]; //是否有效（1有效，0无效）
                 //mMsjHzHkzmbggz.Movesign = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");; //是否传输（1已，0未）
                 mResultModel = MsjHzHkzmbggzBLL.Update(mMsjHzHkzmbggz,context.Session["UserID"], true); //新建操作
                 }
                else
                 {
                mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                mResultModel.ISSucceed = false;               
                 }
                
            context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty);
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }    
        #endregion 
        
        #region 获取实体记录
      /// <summary>
      /// 获取实体记录
      /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
             ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = string.Empty;
            try
            {
                MsjHzHkzmbggz mMsjHzHkzmbggz = new MsjHzHkzmbggz(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mMsjHzHkzmbggz = MsjHzHkzmbggzBLL.Load(paramKey); //根据键值获取管理员
                    if (mMsjHzHkzmbggz != null)
                    {
                        json = JsonConvert.SerializeObject(mMsjHzHkzmbggz);
                        Hashtable hashtable = new Hashtable();
                        if (!string.IsNullOrEmpty(mMsjHzHkzmbggz.Sqrxb))
                        {
                            hashtable.Add("SqrxbTxt", mMsjHzHkzmbggz.Sqrxb == "0" ? "男" : "女");
                        }
                        if (!string.IsNullOrEmpty(mMsjHzHkzmbggz.Enabled))
                        {
                            hashtable.Add("EnabledTxt", mMsjHzHkzmbggz.Enabled == "1" ? "有效" : "无效");
                        }
                        json = JsonString.AddAttribute(json, hashtable);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty);
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            
            context.Response.Write(json);
        }
        #endregion
      
        #region 获取数据
        /// <summary>
      /// 获取数据记录
      /// </summary>
       protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];

            string xm = context.Request.Form["texSqrxm"]; //姓名
            string xb = context.Request.Form["selSqrxb"]; //性别(字典)
            string sfzh = context.Request.Form["texSqrsfzh"]; //身份证号
            string hkszd = context.Request.Form["texHkszd"]; //现户口所在地
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
            { 
                return;
            }

            SqlConditionList conditionList = new SqlConditionList();


            if (!String.IsNullOrEmpty(xm))
            {
                conditionList.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjHzHkzmbggzConst.Columns.Sqrxm, "%" + xm + "%"), SqlLogicOper.AND_LOGIC);
            }

            if (!String.IsNullOrEmpty(xb))
            {
                conditionList.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, MsjHzHkzmbggzConst.Columns.Sqrxb, xb), SqlLogicOper.AND_LOGIC);
            }

            if (!String.IsNullOrEmpty(sfzh))
            {
                conditionList.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjHzHkzmbggzConst.Columns.Sqrsfzh, "%" + sfzh + "%"), SqlLogicOper.AND_LOGIC);
            }

            if (!String.IsNullOrEmpty(hkszd))
            {
                conditionList.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjHzHkzmbggzConst.Columns.Hkszd, "%" + hkszd + "%"), SqlLogicOper.AND_LOGIC);
            }
            string sqlWhere = conditionList.ToSql();
            DataTable dtMsjHzHkzmbggz = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage q=new QueryByPage();
            QueryInfo qInfo=new QueryInfo();
            
            dtMsjHzHkzmbggz = q.GetRecordByPage("*", sqlWhere, string.Empty, intPageSize, intPageIndex, MsjHzHkzmbggzConst.TableName, MsjHzHkzmbggzConst.FieldKey, string.Empty).Tables[0];
            //翻译表DataTable中的特殊字段
            Hashtable columnData = new Hashtable();
            columnData.Add("0", "男");
            columnData.Add("1", "女");

            ResolveColumn resolveColumn = new ResolveColumn();
            resolveColumn.oldColumnName = "SQRXB";
            resolveColumn.newType = typeof(String);
            resolveColumn.columnData = columnData;

            ResolveColumn[] resolveColumnArray = new ResolveColumn[] { resolveColumn };
            dtMsjHzHkzmbggz = ResolveReference.ResolveDataTable(dtMsjHzHkzmbggz, resolveColumnArray);
           
           if (dtMsjHzHkzmbggz != null && dtMsjHzHkzmbggz.Rows.Count > 0)
             {
                 for (int i = 0; i < dtMsjHzHkzmbggz.Columns.Count; i++)
                 {
                     hm = new HearderMessage();
                     hm.name = dtMsjHzHkzmbggz.Columns[i].Caption;
                     list.Add(hm);
                 }
                
                 tm.Header = list;
                 tm.data = dtMsjHzHkzmbggz;
                 tm.totalCount = Convert.ToString((int)q.RowCount);
             }
            
            string json = JsonConvert.SerializeObject(tm);
            context.Response.Write(json);
        }
       
       protected void QueryGtRecord()
        {
           HttpContext context = HttpContext.Current;
           context.Response.ContentType = "text/plain";

           QueryInfo info = new QueryInfo();
           QueryByPage page = new QueryByPage();
           info.FieldKey = MsjHzHkzmbggzConst.FieldKey;
           info.PageIndex = 1;
           info.PageSize = 0;
           info.QTableName = MsjHzHkzmbggzConst.TableName;
           info.SelectFields = "*";

           info.Where = string.Empty;
           DataSet ds = page.GetRecordByPage(info);
           DataTable table = new DataTable();
        
           if (ds.Tables.Count > 0)
           {
               table = page.GetRecordByPage(info).Tables[0];
           }
           string json = JsonConvert.SerializeObject(table);
           context.Response.Write(json);
        }
         #endregion           
}
}
 
 
